/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ordering.dao;

import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.TMenu;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.ordering.pojo.TFoodType;
import com.ordering.utils.ConfigIni;

/**
 *
 * @author sulan
 */
public class TMenuJpaController {

    public TMenuJpaController() {
        emf = EntityManagerUtils.em;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TMenu TMenu) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TFoodType foodTypeOid = TMenu.getFoodTypeOid();
            if (foodTypeOid != null) {
                foodTypeOid = em.getReference(foodTypeOid.getClass(), foodTypeOid.getFoodTypeOid());
                TMenu.setFoodTypeOid(foodTypeOid);
            }
            em.persist(TMenu);
            if (foodTypeOid != null) {
                foodTypeOid.getTMenuCollection().add(TMenu);
                foodTypeOid = em.merge(foodTypeOid);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TMenu TMenu) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TMenu persistentTMenu = em.find(TMenu.class, TMenu.getMenuId());
            TFoodType foodTypeOidOld = persistentTMenu.getFoodTypeOid();
            TFoodType foodTypeOidNew = TMenu.getFoodTypeOid();
            if (foodTypeOidNew != null) {
                foodTypeOidNew = em.getReference(foodTypeOidNew.getClass(), foodTypeOidNew.getFoodTypeOid());
                TMenu.setFoodTypeOid(foodTypeOidNew);
            }
            TMenu = em.merge(TMenu);
            if (foodTypeOidOld != null && !foodTypeOidOld.equals(foodTypeOidNew)) {
                foodTypeOidOld.getTMenuCollection().remove(TMenu);
                foodTypeOidOld = em.merge(foodTypeOidOld);
            }
            if (foodTypeOidNew != null && !foodTypeOidNew.equals(foodTypeOidOld)) {
                foodTypeOidNew.getTMenuCollection().add(TMenu);
                foodTypeOidNew = em.merge(foodTypeOidNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = TMenu.getMenuId();
                if (findTMenu(id) == null) {
                    throw new NonexistentEntityException("The tMenu with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TMenu TMenu;
            try {
                TMenu = em.getReference(TMenu.class, id);
                TMenu.getMenuId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The TMenu with id " + id + " no longer exists.", enfe);
            }
            TFoodType foodTypeOid = TMenu.getFoodTypeOid();
            if (foodTypeOid != null) {
                foodTypeOid.getTMenuCollection().remove(TMenu);
                foodTypeOid = em.merge(foodTypeOid);
            }
            em.remove(TMenu);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TMenu> findTMenuEntities() {
        return findTMenuEntities(true, -1, -1);
    }

    public List<TMenu> findTMenuEntities(int maxResults, int firstResult) {
        return findTMenuEntities(false, maxResults, firstResult);
    }

    private List<TMenu> findTMenuEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TMenu as o order by o.foodName");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TMenu findTMenu(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TMenu.class, id);
        } finally {
            em.close();
        }
    }

    public int getTMenuCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from TMenu as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
